package org.jsrml.core.purchaser.entity;

import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;
import org.jsrml.common.persistence.StringIdBaseEntity;
import org.jsrml.common.util.UUIDGenerator;
import org.jsrml.core.base.entity.BaseStatus;
import org.jsrml.core.purchaser.command.SaveDepartmentCo;

/**
 * @author 张剑
 */
@DynamicUpdate
@Entity
@Table(name = M.TABLE_PREFIX + "DEPARTMENT")
@SuppressWarnings("serial")
public class Department extends StringIdBaseEntity {
	
	@Column(name = "DEPARTMENT_CODE", length = 64)
	private String departmentCode;
	
	@Column(name = "DEPARTMENT_NAME", length = 32)
	private String departmentName;
	
	/**
	 * 公司
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "COMPANY_ID",foreignKey = @ForeignKey(name = "none"))
	private Company company;
	
	/**
	 * 上级部门
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PARENT_DEPT_ID",foreignKey = @ForeignKey(name = "none"))
	private Department parentDept;
	
	/**
	 * 下级子科室
	 */
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "parentDept")
	@OrderBy("id")
	private Set<Department> childDept;
	
	
	@Column(name = "DESCRIPT", columnDefinition = M.TEXT_COLUMN)
	private String descript;
	
	private BaseStatus status;

	public void create(SaveDepartmentCo command,Department parentDept,Company company){
		setId(UUIDGenerator.getUUID());
		
		setDepartmentCode(command.getDepartmentCode());
		setDepartmentName(command.getDepartmentName());
		setCompany(company);
		setParentDept(parentDept);
		setDescript(command.getDescript());
		setStatus(new BaseStatus());
		getStatus().setRemove(false);
		getStatus().setCreateDate(new Date());
	}
	
	public void modify(SaveDepartmentCo command,Department parentDept,Company company){
		
		setDepartmentCode(command.getDepartmentCode());
		setDepartmentName(command.getDepartmentName());
		setCompany(company);
		setParentDept(parentDept);
		setDescript(command.getDescript());
		
		if(getStatus()==null){
			setStatus(new BaseStatus());
		}
		getStatus().setUpdateDate(new Date());
	}
	
	public void remove() {
		getStatus().setRemove(true);
	}
	
	public String getDepartmentCode() {
		return departmentCode;
	}

	public void setDepartmentCode(String departmentCode) {
		this.departmentCode = departmentCode;
	}

	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public Department getParentDept() {
		return parentDept;
	}

	public void setParentDept(Department parentDept) {
		this.parentDept = parentDept;
	}

	public Set<Department> getChildDept() {
		return childDept;
	}

	public void setChildDept(Set<Department> childDept) {
		this.childDept = childDept;
	}

	public String getDescript() {
		return descript;
	}

	public void setDescript(String descript) {
		this.descript = descript;
	}

	public BaseStatus getStatus() {
		return status;
	}

	public void setStatus(BaseStatus status) {
		this.status = status;
	}
}
